package cn.edu.zut.sunday.misc.controller;

import cn.edu.zut.sunday.base.controller.GenericController;
import cn.edu.zut.sunday.misc.domain.InformationGroup;
import cn.edu.zut.sunday.misc.domain.InformationSecurity;
import cn.edu.zut.sunday.misc.service.InformationGroupService;
import cn.edu.zut.sunday.misc.vo.InformationGroupQuery;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api("HXC-信息安全数据接口")
@Slf4j
@RestController
@RequestMapping("infoGroup")
@CrossOrigin
public class InfoGroupController extends GenericController<InformationGroup, Integer, InformationGroupService> {
    @Autowired
    public void setInformationGroupService(InformationGroupService informationGroupService) {
        this.service = informationGroupService;
    }
    @Override
    protected Pageable createPageable(int page, int size) {
        Sort sort  =  Sort.by(Sort.Direction.ASC,"id");
        return PageRequest.of(page, size, sort);
    }
    @Override
    protected Specification<InformationGroup> createSpecification(String requestBodyStr) {
        log.info("requestBodyStr: {}", requestBodyStr);
        final InformationGroupQuery informationGroupQuery = new Gson().fromJson(requestBodyStr,
                InformationGroupQuery.class);
        log.info("InformationQuery: {}", informationGroupQuery);

        if (informationGroupQuery != null) {
            return getInformationGroupSpecification(informationGroupQuery);
        }
        return null;
    }
    //分页条件查询
    private Specification<InformationGroup> getInformationGroupSpecification(
            InformationGroupQuery informationGroupQuery) {
        String   cveId    = informationGroupQuery.getCveId();
        String vulnType = informationGroupQuery.getVulnType();

        Specification<InformationGroup> specification = (root, query, criteriaBuilder) -> {
            query.distinct(true);
            List<Predicate> list = new ArrayList<>();

            if(!StringUtils.isEmpty(cveId )){
                Predicate p1 = criteriaBuilder.like(root.get("cveId"), sqlLike(cveId));
                list.add(p1);
            }

            if(!StringUtils.isEmpty(vulnType)){
                // 此处为查询serverName中含有key的数据
                Predicate p2 = criteriaBuilder.like(root.get("vulnType"), sqlLike(vulnType));
                list.add(p2);
            }

            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };

        return specification;
    }

    @ApiOperation(value = "查询所有数据")
    @GetMapping(value = "/findAll")
    @ResponseBody
    public List findAll() {

      List<InformationGroup> list =  this.service.findAll();
        return list;
    }


}
